//
//  HXGiftCollectionCell.m
//  GiftSay
//
//  Created by laohu on 16/1/7.
//  Copyright © 2016年 liyinkai. All rights reserved.
//

#import "HXGiftCollectionCell.h"
#import "HXGiftFirstModel.h"
#import "HXGiftTableViewCell.h"
#import "HXGiftCollectionViewCell.h"

@interface HXGiftCollectionCell ()
@property (nonatomic, assign) BOOL ifCollection;
@end

@implementation HXGiftCollectionCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.oldIndex = [NSIndexPath indexPathForRow:0 inSection:0];
//        self.presentIndex = [NSIndexPath indexPathForRow:0 inSection:0];
        
        self.point = [self.giftCollection convertPoint:CGPointMake((self.width - self.giftTableView.width) / 2, self.height * 4 / 5) toView:self];
        [self createView];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"NightModeOpen" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"NightModeOff" object:nil];
//        [self getSectionHeight];
        
    }
    return self;
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)refresh {
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
        self.giftTableView.backgroundColor = kNightModeColor1;
//        self.giftCollection.backgroundColor = kNightModeColor1;
        self.god.backgroundColor = kNightModeColor;
        [self.god setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    } else {
        self.giftTableView.backgroundColor = [UIColor whiteColor];
//        self.giftCollection.backgroundColor = [UIColor whiteColor];
        self.god.backgroundColor = [UIColor whiteColor];
        [self.god setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    [self.giftTableView reloadData];
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super applyLayoutAttributes:layoutAttributes];
    self.god.frame = CGRectMake(0, 0, self.width, self.height / 10);
    self.giftTableView.frame = CGRectMake(0, self.god.bottom, self.width * 0.25, self.height * 9 /10);
    self.giftCollection.frame = CGRectMake(self.giftTableView.right, self.god.bottom, self.width * 0.75, self.height * 9 / 10);
}

//创建攻略tableview
- (void)createView {
    
    self.god = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.god setTitle:@"——————————选礼神器——————————" forState:UIControlStateNormal];
    [self.god setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    self.god.titleLabel.font = [UIFont systemFontOfSize:15 * SCREEN_WIDTH];
//    self.god.backgroundColor = [UIColor whiteColor];
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
        self.god.backgroundColor = kNightModeColor1;
        [self.god setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }  else {
        self.god.backgroundColor = [UIColor whiteColor];
        [self.god setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    self.god.layer.borderWidth = 0.5;
    [self.god addTarget:self action:@selector(jump:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_god];
    
    
    self.giftTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
        self.giftTableView.backgroundColor = kNightModeColor1;
    }  else {
        self.giftTableView.backgroundColor = [UIColor whiteColor];
    }
//    self.giftTableView.backgroundColor = [UIColor whiteColor];
    self.giftTableView.delegate = self;
    self.giftTableView.dataSource = self;
    self.giftTableView.decelerationRate = 5.0;
    [self.giftTableView registerClass:[HXGiftTableViewCell class] forCellReuseIdentifier:@"GiftTableViewReUseIdentifier"];
    [self addSubview:_giftTableView];
    
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.flowLayout.itemSize = CGSizeMake(0.25 * self.width, 0.27 * self.height);
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.minimumLineSpacing = 0;
    
    self.giftCollection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
    self.giftCollection.backgroundColor = [UIColor whiteColor];

    self.giftCollection.delegate = self;
    self.giftCollection.dataSource = self;
    self.giftCollection.bounces = NO;
    self.giftCollection.decelerationRate = 0.0;
    [self.giftCollection registerClass:[HXGiftCollectionViewCell class] forCellWithReuseIdentifier:@"GiftCollection"];
    [self.giftCollection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionViewHead"];
    self.giftCollection.backgroundColor = [UIColor whiteColor];
    [self addSubview:_giftCollection];
    
    
    
}
- (void)jump:(UIButton *)sender {
//    NSLog(@"qweq");
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"XuanLiShenQi" object:nil];
}
#pragma mark - TableView
- (NSInteger)
tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    NSArray *arr = [self.giftData[section] subcategory];
//    NSLog(@"🐱%ld", arr.count);
    return self.giftData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HXGiftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GiftTableViewReUseIdentifier"];
//    cell.backgroundColor = [UIColor whiteColor];
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
        cell.label.backgroundColor = kNightModeColor1;
    } else {
        cell.label.backgroundColor = [UIColor whiteColor];
    }
    cell.label.text = [self.giftData[indexPath.row] name];

    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.height / 11;
}

#pragma mark - collectionview
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSArray *arr = [self.giftData[section] subcategory];
    return arr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HXGiftCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"GiftCollection" forIndexPath:indexPath];
    HXGiftFirstModel *first = self.giftData[indexPath.section];
    
    NSArray *array = first.subcategory;
    item.secondModel = array[indexPath.item];
    item.backgroundColor = [UIColor whiteColor];
    return item;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HXGiftFirstModel *first = self.giftData[indexPath.section];
    NSNumber *ids = [first.subcategory[indexPath.row] ids];
//    NSString *str = [NSString stringWithFormat:kItemData, number.stringValue];
    NSString *name = [first.subcategory[indexPath.row] name];
    NSArray *arr = [NSArray arrayWithObjects:ids, name, nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"JumpToHot" object:arr];
}
//返回分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.giftData.count;
}
//collection返回分区头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *reuseView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionViewHead" forIndexPath:indexPath];
        [reuseView removeAllSubviews];
        if (indexPath.section != 0) {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width * 3 / 4, 70)];
            label.textAlignment = NSTextAlignmentCenter;
            NSString *str = [NSString stringWithFormat:@"—————————— %@ ——————————", [self.giftData[indexPath.section] name]];
            label.text = str;
            label.font = [UIFont systemFontOfSize:10 * SCREEN_WIDTH];
            label.textColor = [UIColor grayColor];
            [reuseView addSubview:label];
        }
        
        reuseView.backgroundColor = [UIColor whiteColor];
        
        return reuseView;
    } else {
        UICollectionReusableView *resableviewtow = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath ];
        return resableviewtow;
    }
}

// 头视图高度
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return CGSizeZero;
    } else {
    return  CGSizeMake(self.width / 5 * 4, 70);
    }
}

#pragma mark - relative
//tablebiew点击事件

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.ifCollection = YES;
    for (int i = 0; i < self.giftData.count; i++) {
        NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
        HXGiftTableViewCell *cell = [tableView cellForRowAtIndexPath:path];
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
            cell.label.textColor = [UIColor whiteColor];
        } else {
            cell.label.textColor = [UIColor blackColor];
        }
        
    }
    //点击的cell
    HXGiftTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.label.textColor = [UIColor redColor];
    
    //collection目标indexpath
    NSIndexPath *path = [NSIndexPath indexPathForItem:indexPath.section inSection:indexPath.row];
    
    [self.giftCollection scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
    //记录点击的indexpath
    self.oldIndex = indexPath;

    
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    //tableview cell将要出现  -----解决重用问题
    HXGiftTableViewCell *thisCell = (HXGiftTableViewCell *)cell;
    if (indexPath.row == self.oldIndex.row) {
        thisCell.label.textColor = [UIColor redColor];
    } else {
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
            thisCell.label.textColor = [UIColor whiteColor];
        } else {
            thisCell.label.textColor = [UIColor blackColor];
        }
        
    }
}



//collection判断item将要出现
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@"%ld, %ld", indexPath.section, indexPath.row);
    
        //将要出现的item的section = tableview滑动的row
        NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.section inSection:0];
        //将tableview全部变黑
        for (int i = 0; i < self.giftData.count; i++) {
            NSIndexPath *path2 = [NSIndexPath indexPathForItem:i inSection:0];
            HXGiftTableViewCell *oldCell = [self.giftTableView cellForRowAtIndexPath:path2];
            if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
                oldCell.label.textColor = [UIColor whiteColor];
            } else {
                oldCell.label.textColor = [UIColor blackColor];
            }
            
        }
        //label 变色
    if (self.ifCollection == NO) {
        HXGiftTableViewCell *cell3 = [self.giftTableView cellForRowAtIndexPath:path];
        cell3.label.textColor = [UIColor redColor];
        //将变色的label给oldindex
        self.oldIndex = path;
    } else {
        HXGiftTableViewCell *cell2 = [self.giftTableView cellForRowAtIndexPath:self.oldIndex];
        cell2.label.textColor = [UIColor redColor];
    }
    
    //tableview row滚到collection 将要出现的section
        [self.giftTableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionNone animated:YES];
    
}


-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.giftCollection) {
        self.ifCollection = NO;
    }
}










@end
